<template>
  <a-card :bordered="false" class="br-10">
    <div class="table-page-search">
      <a-form>
        <a-form-item label="字典名称">
          <a-input placeholder="请输入" v-model="queryParam.dictName" />
        </a-form-item>
        <a-form-item label="字典类型">
          <a-input placeholder="请输入" v-model="queryParam.dictType" />
        </a-form-item>
        <a-form-item label="状态">
          <a-select placeholder="请选择" v-model="queryParam.status" allowClear>
            <a-select-option
              v-for="dict in Dict.getLocal('statusTextDict')"
              :key="dict.dictValue"
              :value="dict.dictValue"
            >
              {{ dict.dictLabel }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="() => (queryParam = { status: undefined })">重置</a-button>
        </span>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button v-has="'system:dict:add'" type="primary" @click="$refs.modal.add()">新建</a-button>
      <a-dropdown v-has="'system:dict:remove'" v-if="selectedRowKeys.length > 0">
        <a-button type="danger" icon="delete" @click="delByIds(selectedRowKeys)">删除</a-button>
      </a-dropdown>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="dictId"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data="loadData"
    >
      <span slot="status" slot-scope="text">
        <DictTag :options="Dict.getLocal('statusTextDict')" :value="text" />
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button
          v-has="'system:dict:edit'"
          type="primary"
          ghost
          size="small"
          class="_mr-5"
          @click="handleEdit(record)"
        >
          编辑
        </a-button>
        <a-button
          v-has="'system:dict:edit'"
          type="primary"
          ghost
          size="small"
          class="_mr-5"
          @click="dataModal(record.dictType)"
        >
          列表
        </a-button>
        <a-button
          v-has="'system:dict:remove'"
          type="primary"
          ghost
          size="small"
          class="_mr-5"
          @click="delByIds([record.dictId])"
        >
          删除
        </a-button>
      </span>
    </s-table>
    <dict-modal ref="modal" @ok="handleOk" />
    <dict-data-list-modal ref="datamodal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getDictTypeList, delDictType } from '@/api/system/system'
import DictModal from './modules/DictModal.vue'
import DictDataListModal from './modules/DictDataListModal.vue'
import DictTag from '@/components/DictTag'

export default {
  name: 'TableList',
  components: {
    STable,
    DictModal,
    DictDataListModal,
    DictTag
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      permissions: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        status: undefined
      },
      // 表头
      columns: [
        {
          title: '字典主键',
          dataIndex: 'dictId'
        },
        {
          title: '字典名称',
          dataIndex: 'dictName'
        },
        {
          title: '字典类型',
          dataIndex: 'dictType'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '备注',
          dataIndex: 'remark'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          sorter: true
        },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getDictTypeList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {},
  methods: {
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleAdd(parentId) {
      this.$refs.modal.add(parentId)
    },
    handleEdit(record) {
      this.$refs.modal.edit(record)
    },
    handleOk() {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    dataModal(dictType) {
      this.$refs.datamodal.show(dictType)
    },
    delByIds(ids) {
      var _this = this
      this.$confirm({
        title: '权限删除?',
        content: '是否确定删除该权限?',
        onOk() {
          delDictType({ ids: ids.join(',') }).then(res => {
            if (res.code === 0) {
              _this.$message.success(`删除成功`)
              _this.handleOk()
            } else {
              _this.$message.error(res.msg)
            }
            _this.selectedRowKeys = []
          })
        },
        onCancel() {}
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
